<?php

namespace App\Http\Controllers;
use DB;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
   /*
        执行类别的添加
        /admin/cate/add
   */

    public function getAdd($id=''){
    	// dd($id);
    	//调用静态方法获取
    	$data = self::getCates();
    	// dd();
        return view('cate.add',['list'=>$data,'id'=>$id]);
    }

    /*
		格式化处理类别的信息

    */
		public static function getCates(){

    	//查询类别信息

    	// $data = DB::table('cates')->get();

    	$data = DB::select('select *,concat(path,",",id) as paths from cates order by paths');
    	//处理分页
    	foreach($data as $k=>$v){
    		// echo '|----'.$v['name'].'<br>';
    		$len = explode(',',$v['path']);
    		$num = count($len)-1;
    		// echo $num.'|--'.$v['name'].'<br>';
    		$data[$k]['name'] = str_repeat('|---',$num).$v['name'];

    	}
    	return $data;
	}

    /*
		执行子类的插入
		/admin/cate/insert
    */
	public function postInsert(Request $request){
		$data['name'] = $request->input('name');
		//判断添加子类还是父类
		if(!$request->input('pid')==0){
			//添加顶级父类
			// $data['name']=$request->input('name');
			//添加pid所对应的子类
			// $data['name'] = $request->input('name');
			$data['pid'] = $request->input('pid');//$request -> input('pid') ----->父类的id
			//查询父类的path
			$res = DB::table('cates')->where('id',$request->input('pid'))->first();
			//封装子类的path
			$data['path'] = $res['path'].','.$res['id'];
			// $result = DB::table('cates')->insert($data);
		}
		$result = DB::table('cates')->insert($data);
		if($result){
				//跳转到类别浏览页面
				return redirect('admin/cate/index')->with('success','添加类别成功');
			}else{
				return back()->with('error','添加失败');
			}
	}

	/*
		浏览类别列表
		/admin/cate/index
	*/
	public function getIndex(){
		$data = DB::select('select *,concat(path,",",id) as paths from cates order by paths');
        return view('cate.index',['list'=>$data]);
	}

	/*
		类的删除 (如果一个父类不面有子类，不能直接删除父类，必须先把子类清空才能删除父类)
		/admin/cate/del
	*/
	public function getDel($id){
		//判断这个父类是否有子类
		$res = DB::table('cates')->where('pid','=',$id)->count();
		if($res>0){
			//代表这个类与面的子类
			return  back()->with('error','该类下面有子类请先把,该父类下面的子类删除在删除该父类');
		}else{
			$res = DB::table('cates')->where('id','=',$id)->delete();
			if($res){
				return redirect('/admin/cate/index')->with('success','类别删除成功');
			}else{
				return bach()->with('error','类别删除失败');
			}
		}
	}

	/*
		修改类别信息(只能修改类别的名称,不能修改类别的父类)
		/admin/cate/edit/{id}
		
	*/

	public function getEdit($id){
		//调用静态方法获取类的信息
		$data = self::getCates();
		//获取该类父类的id，目的是为了让该 类父类在下框中选中
		$res = DB::table('cates')->where('id','=',$id)->first();
		return view('cate.edit',['list'=>$data,'res'=>$res]);
	}

	/*
		执行修改
		/admin/cate/update
	*/
	public function postUpdate(Request $request){
		// dd($request->all());
		$data = DB::table('cates')->where('id','=',$request->input('id'))->update(array('name'=>$request->input('name')));
		if($data){
			return redirect('/admin/cate/index')->with('success','修改成功');
		}else{
			return back()->with('error','修改失败');
		}
	}


	/*
		处理父子类关系

	*/

	// public static function getCatesBypid($pid=0){
	// 	//获取$pid下面的所有子类
	// 	$res = DB::table('cates')->where('pid','=',$pid)->get();
	// 	$data = [];
	// 	foreach($res as $k=>$v){
	// 	// echo $v['name'].'----'.$v['id'].'<br>';
	// 		$v['sub']=self::getCatesBypid($v['id']);
	// 		$data[]=$v;
	// 	}
	// 	return $data;
	// }

	// public function getTest(){
	// 	dd(self::getCatesBypid(0));
	// }

	/*
		优化递归
		
	*/
	//获取所有cates数据
	public static function getAllCates(){
		return DB::table('cates')->get();
	}

	//根据PID获取所有的子类
	public static function getCatesByPidArr($cates,$pid){
		$data = [];
		foreach($cates as $k=>$v){
			if($v['pid']==$pid){//判断当前查询的父类id
				$v['sub']=self::getCatesByPidArr($cates,$v['id']);
				$data[]=$v;
			}
		}
		return $data;
	}
	//获取所的类别
	public static function getCatesInfo(){
		//获取所的类别数据
		$cates = self::getAllCates();
		$res = self::getCatesByPidArr($cates,0);
		dd($res);
	}

	public function getTest(){
		self::getCatesInfo();
	}
}